package com.zywl.api;

import com.github.pagehelper.util.StringUtil;
import com.zywl.core.beans.Company;
import com.zywl.core.beans.Dictionary;
import com.zywl.core.beans.User;
import com.zywl.service.CompanyService;
import com.zywl.service.DictionaryService;
import com.zywl.service.MemberService;
import com.zywl.utils.models.AjaxResult;
import com.zywl.utils.models.JsonResult;
import com.zywl.utils.models.PageData;
import com.zywl.utils.models.PageResult;
import com.zywl.utils.utils.Constants;
import com.zywl.utils.utils.MapUtil;
import com.zywl.utils.utils.enums.LogTypeEnum;
import com.zywl.web.controller.base.BaseController;
import com.zywl.web.controller.form.CompanyForm;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/company/")
public class CompanyApi extends BaseController {
    @Autowired
    private CompanyService companyService;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private MemberService memberService;

    @ResponseBody
    @PostMapping("create")
    public JsonResult create(Company form) {
        JsonResult jr = new JsonResult();
        Date date = new Date();
        if (form != null) {
            try {
                if(StringUtils.isEmpty(form.getComType())){
                    return jr.error("类型不能为空！");
                }
                if("2".equals(form.getComType())){
                    Map<String,Object> map = new HashMap<>();
                    map.put("comStt","0");
                    map.put("comName",form.getComName());
                    int count = companyService.getCountByParams(map);
                    if (count > 0){
                        return jr.error("该公司名称已经存在！");
                    }
                }
                if(StringUtils.isNotEmpty(form.getComIndustry())){
                    form.setComIndustry(","+form.getComIndustry()+",");
                }
                if(StringUtils.isNotEmpty(form.getComTag())){
                    form.setComTag(","+form.getComTag()+",");
                }
                Company company = new Company();
                form.setComStt("0");
                form.setComCreateTime(date);
                form.setComModifyTime(date);
                BeanUtils.copyProperties(form, company);
                companyService.save(company);
                jr.success("添加成功");
            } catch (Exception e) {
                logger.error("提交公司信息异常", e);
                jr.error("提交公司信息异常");
            }
        } else {
            jr.error("提交失败，参数为空");
        }
        return jr;
    }

    @ResponseBody
    @PostMapping("detail")
    public JsonResult detail(String id) {
        JsonResult jr = new JsonResult();
        try {
            Company company = companyService.getById(Long.valueOf(id));
            CompanyForm companyForm = new CompanyForm();
            BeanUtils.copyProperties(company, companyForm);
            List<Dictionary> dictionaryList = dictionaryService.getDictionaryByType("indu");
            List<Dictionary> tagsList = dictionaryService.getDictionaryByType("tags");
            Map<String,String> map = new HashMap<>();
            for (Dictionary dictionary : dictionaryList){
                map.put(dictionary.getDisCode(),dictionary.getDisCodeName());
            }
            Map<String,String> tagmap = new HashMap<>();
            for (Dictionary dictionary : tagsList){
                tagmap.put(dictionary.getDisCode(),dictionary.getDisCodeName());
            }
            if(StringUtils.isNotEmpty(company.getComIndustry())){
                String[] indus = company.getComIndustry().split(",");
                String induName = "";
                for (String str : indus){
                    if(StringUtil.isNotEmpty(str)){
                        induName += map.get(str) + ",";
                    }
                }
//                companyForm.setComIndustry(company.getComIndustry().substring(1,company.getComIndustry().length()-1));
                companyForm.setComIndustry(induName.substring(0,induName.length()-1));
                companyForm.setComIndustryName(induName.substring(0,induName.length()-1));
            }

            if(StringUtils.isNotEmpty(company.getComTag())){
                String[] tags = company.getComTag().split(",");
                String tagsName = "";
                for (String str : tags){
                    if(StringUtil.isNotEmpty(str)){
                        tagsName += tagmap.get(str) + ",";
                    }
                }
                companyForm.setComTag(company.getComTag().substring(1,company.getComTag().length()-1));
                companyForm.setComTagName(tagsName.substring(0,tagsName.length()-1));
            }
            jr.success("查询成功",companyForm);
        } catch (Exception e) {
            logger.error("查询详情失败", e);
            jr.error("查询失败");
        }
        return jr;
    }

}
